package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/sys_menu")
@Slf4j
public class SysMenuController {
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 查找所有
     * @return
     */
    @GetMapping("/allData")
    public ResultData getAll() {
        //查询pid为0的菜单
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, 0);
        List<SysMenu> sysMenuList = sysMenuService.list(queryWrapper);
        List<SysMenuDto> sysMenuDtoList = sysMenuList.stream().map(item -> {
            SysMenuDto menuDto = new SysMenuDto();
            BeanUtils.copyProperties(item, menuDto);
            //通过递归查询子菜单和子菜单的子菜单
            this.getZimenu(menuDto);
            return menuDto;

        }).collect(Collectors.toList());
        return ResultData.ok("",sysMenuDtoList);

    }

    private void getZimenu(SysMenuDto menuDto) {
        //获取id
        Integer id = menuDto.getId();
        //通过id查询子菜单,将子菜单封装
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, id);
        List<SysMenu> sysMenuList = sysMenuService.list(queryWrapper);
        //判断查询的子菜单是否为空
        if (CollectionUtils.isEmpty(sysMenuList)) {
            //为空说明没有子菜单
            return;
        } else {
            List<SysMenuDto> sysMenuDtoList = sysMenuList.stream().map(item -> {
                SysMenuDto sysMenuDto = new SysMenuDto();
                BeanUtils.copyProperties(item,sysMenuDto );
                getZimenu(sysMenuDto);
                return sysMenuDto;
            }).collect(Collectors.toList());
            //封装
            menuDto.setChildren(sysMenuDtoList);
        }

    }

    /**
     * 添加菜单
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody SysMenu sysMenu, HttpServletRequest request){
        log.info("sysMenu:+",sysMenu);

        sysMenuService.save(sysMenu);
        return R.success("添加成功","");
    }

    /**
     * 数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id) {

        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getId,id);
        SysMenu sysMenu   = sysMenuService.getOne(queryWrapper);
        //  = yddLinkService.getById(id);
        return ResultData.ok("",sysMenu);

    }
    /**
     * 修该02
     * @param
     * @return
     */
    @PostMapping("/update")
    public R<String> update(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return R.success("修改成功","");
    }
    /**
     * 删除链接
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R<String> delete(String id){
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getPid, id);
        List<SysMenu> list = sysMenuService.list(queryWrapper);
        if (list.size() > 0) {
            //若存在则不删除
            throw new BusinessException("存在子目录,不能删除");
        }
        sysMenuService.removeById(id);
        return R.success("删除成功","");
    }

}
